# -*- coding: utf-8 -*-
import json

def getcompanyMainList(request):
    
    #1数据
    request.mysql_52.execute(request.formatSql('''
        select 
            company_name,
            social_credit,
            entry_sign,
            province,
			city,
            legal_person,
            company_type,
            registered_capital,
            registered_date,
            lxr,
            registered_address,
            business_address,
			url
        from middle.company_main_${data_source} 
        where  company_name like '%${company_name}%'
        and ('${entry_sign}' = 'all' or entry_sign = '${entry_sign}')
        and ('${province}' = '' or province like '%${province}%')
        limit ${start} , ${rows}
    '''))
    rows = request.mysql_52.fetchall()
    
    #2页码
    request.mysql_52.execute(request.formatSql('''
        select count(*)  as total
        from middle.company_main_${data_source}
        where  company_name like '%${company_name}%'
        and ('${entry_sign}' = 'all' or entry_sign = '${entry_sign}')
        and ('${province}' = '' or province like '%${province}%')
    '''))
    total = request.mysql_52.fetchone()['total']
    #print({'rows':rows,'total':total})
    return json.dumps({'rows':rows,'total':total}, ensure_ascii=False)


#删除一个企业相关的人员和资质
def deleteAllByCompanyId(request):
    delete_count = 0
    data_source = request.form['data_source']
    company_name_list = request.form['company_name_list']

    for company_name in company_name_list.split("^"):

        sql = "delete from person_apt_${data_source}  where person_id like  '" + company_name +"^%' )"
        #print(sql)
        request.mysql_52.execute(request.formatSql(sql))
        delete_count = delete_count + request.mysql_52.rowcount

        sql = "   delete  from person_main_${data_source} where person_id like  '" + company_name + "^%'"
        request.mysql_52.execute(request.formatSql(sql))
        delete_count = delete_count + request.mysql_52.rowcount

        sql = "  delete from company_apt_${data_source} where company_name = '" + company_name + "'"
        request.mysql_52.execute(request.formatSql(sql))
        delete_count = delete_count + request.mysql_52.rowcount

        sql = " delete from company_main_${data_source} where company_name = '" + company_name + "'"
        request.mysql_52.execute(request.formatSql(sql))
        delete_count = delete_count + request.mysql_52.rowcount

    return str(delete_count)